<template>
  <!-- 防止接口还没请求完，从goodsInfo对象上读取属性读取不到的情况 -->
  <view v-if="goodsInfo.price">
    <swiper
      class="swiper"
      circular 
      indicator-dots 
      autoplay
      :interval="3000"
    	:duration="1000"
    >
      <swiper-item v-for="(item, index) in goodsInfo.swiperList" :key="index">
        <image :src="item.url" mode="aspectFill" @click="previewImg(index)"></image>
      </swiper-item>
    </swiper>
    <view class="goods-info-box">
      <view class="price">￥{{ goodsInfo.price }}</view>
      <view class="goods-info-body">
        <view class="goods-name">{{ goodsInfo.goodsname }}</view>
        <view class="favi">
          <uni-icons type="star" size="18" color='gray'></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="free">快递：免运费</view>
    </view>
    <!-- 商品导航区 -->
    <view class="goods-carts">
      <uni-goods-nav
        :fill="true"
        :options="options"
        :button-group="buttonGroup"
        @click="onClick"
        @buttonClick="buttonClick"
      />
    </view>
  </view>
</template>
<script setup>
  
  import { onLoad } from '@dcloudio/uni-app'
  import { ref, computed, watch } from 'vue'
  import * as mockData from '../../api/mock.js'
  import { useStore } from 'vuex';
  const store = useStore(); 
  
  const goodsInfo = ref({});
  const options = ref([
    {
      icon: 'shop',
      text: '店铺'
    },{
      icon: 'cart',
      text: '购物车',
      info: 0
    }
  ]);
  const buttonGroup = ref([
    {
      text: '加入购物车',
      backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
      color: '#fff'
    },
    {
      text: '立即购买',
      backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
      color: '#fff'
    }
  ])

  const total = computed(() => {
    return store.getters['cart/total'];
  })
  watch(() => total.value, (newVal) => {
    options.value[1].info = newVal;
  }, {
    immediate: true,
  })
  onLoad((option) => {
    getGoodsInfo(option);
    // console.log('详情页11111', cart.value);
  })
  function getGoodsInfo(query) {
    // const { data: res } = await uni.$http.get('/api/public/v1/goods/detail', query);
    // if(Object.prototype.toString.call(res) !== '[object Object]' || res?.meta?.status !== 200) {
    //   return uni.$showMsg();
    // }
    // goodsInfo.value = res.message;
    goodsInfo.value = mockData.goodsInfo;
  }
  function previewImg(i) {
    uni.previewImage({
      current: i,
      urls: goodsInfo.value.swiperList.map((item) => item.url),
    })
  }
  function onClick({ index, content }) {
    if(index === 1) {
      uni.switchTab({
        url: "/pages/cart/cart"
      })
    }
  }
  function buttonClick({ index, content }) {
    if(index === 0) {
      const goods = {
        id: goodsInfo.value.id,
        price: goodsInfo.value.price,
        goodsname: goodsInfo.value.goodsname,
        logo: goodsInfo.value.logo,
        count: 1,
        state: false,
      }
      store.commit('cart/addToCart', goods);
    }
  }
</script>

<style lang="scss" scoped>
swiper {
  height: 750rpx;
  image {
    height: 100%;
    width: 100%;
  }
}
.goods-info-box {
  padding: 10px 0 10px 10px;
  .price {
    color: #c00000;
    font-size: 18px;
    margin: 10px 0;
  }
  .goods-info-body {
    display: flex;
    justify-content: space-between;
    .goods-name {
      font-size: 13px;
      margin-right: 10px;
    }
    .favi {
      width: 120px;
      font-size: 12px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      border-left: 1px solid #efefef;
      color: gray;
    }
  }
  .free {
    font-size: 12px;
    color: gray;
    margin: 10px 0;
  }
}
.goods-carts {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
}
</style>
